<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')"
			   align-center :close-on-click-modal="false" :close-on-press-escape="mode != 'show' ? false : true">
		<el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="120px" label-position="right" style="height:400px;overflow:auto;overflow-x:hidden;">
			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="单据日期" prop="other_date">
						<el-date-picker v-model="form.other_date" type="date" placeholder="请选择日期" :value-format="'YYYY-MM-DD'" style="width:100%"/>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="单据编号" prop="other_code">
						<el-input v-model="form.other_code" placeholder="请输入单据编号" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-col :span="12">
				<el-form-item label="选择收款单位">
					<el-radio-group v-model="form.pay_type" class="ml-4" @change="radioChange">
						<el-radio :label="1" size="large">客户</el-radio>
						<el-radio :label="2" size="large">供应商</el-radio>
						<el-radio :label="3" size="large">其他</el-radio>
					</el-radio-group>
				</el-form-item>
			</el-col>
			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="收款单位" v-if="form.pay_type == 1" prop="customer_id">
						<el-select v-model="form.customer_id" filterable clearable placeholder="请选择客户" style="width:100%" @change="customerChange">
							<el-option v-for="item in clientList" :key="item.id" :label="item.customer_name" :value="item.id"/>
						</el-select>
					</el-form-item>
					<el-form-item label="收款单位" v-if="form.pay_type == 2" prop="supplier_id">
						<el-select v-model="form.supplier_id" filterable clearable placeholder="请选择供应商" style="width:100%" @change="supplierChange">
							<el-option v-for="item in applyList" :key="item.apply_id" :label="item.apply_name" :value="item.apply_id"></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="收款单位" v-if="form.pay_type == 3" prop="pay_target">
						<el-input v-model="form.pay_target" placeholder="请输入收款单位" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="支出类型" prop="type">
						<el-select v-model="form.type" filterable clearable placeholder="请选择" style="width:100%">
							<el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"/>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="1">
				<el-col :span="12">
					<el-form-item label="结算账户" prop="settlement_id">
						<el-select v-model="form.settlement_id" filterable clearable placeholder="请选择账户" style="width:100%">
							<el-option v-for="item in accountList" :key="item.id" :label="item.account" :value="item.id"/>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="金额" prop="money">
						<el-input v-model="form.money" placeholder="请输入金额" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" placeholder="请输入备注" clearable type="textarea"></el-input>
			</el-form-item>
			<el-form-item label="附件" prop="enclosure">
				<sc-upload-file v-model="form.enclosure" :is_same_name="1">
					<el-button type="primary" icon="el-icon-upload">选择文件</el-button>
				</sc-upload-file>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit(0)">保 存</el-button>
			<el-button v-if="mode == 'add'" type="primary" :loading="isSaveing" @click="submit(1)">保存并审核</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增其他支出',
				edit: '编辑其他支出',
				show: '查看'
			},
			visible: false,
			isSaveing: false,
			//表单数据
			form: {
				id: "",
				other_code: "",
				other_date: this.moment().format('YYYY-MM-DD'),
				customer_id: "",
				supplier_id: "",
				settlement_id: "",
				money: "",
				enclosure: "",
				remark: "",
				other_type: 2,
				pay_target: '',
				type: '',
				pay_type:1
			},
			//验证规则
			rules: {
				other_date: [{required: true, message: '请选择单据日期'}],
				settlement_id: [{required: true, message: '请选择结算账户'}],
				money: [{required: true, message: '请输入金额'}],
			},
			accountList: [],
			typeList: [],
			clientList: [],
			applyList: [],
		}
	},
	watch: {
		// 监听 form.pay_type 的变化
		'form.pay_type'(newPayType) {
			// 根据 newPayType 动态设置验证规则
			if (newPayType === 1) {
				// 当 pay_type 为 1 时，设置 customer_id 为必填，清除 supplier_id 的必填规则
				this.rules.customer_id = [{ required: true, message: '请选择客户' }];
				this.rules.supplier_id = [];  // 清除供应商的必填规则
				this.rules.pay_target = [];    // 清除收款单位的规则
			} else if (newPayType === 2) {
				// 当 pay_type 为 2 时，设置 supplier_id 为必填，清除 customer_id 的必填规则
				this.rules.supplier_id = [{ required: true, message: '请选择供应商' }];
				this.rules.customer_id = [];  // 清除客户的必填规则
				this.rules.pay_target = [];    // 清除收款单位的规则
			} else if (newPayType === 3) {
				// 当 pay_type 为 3 时，设置 pay_target 为必填，清除 customer_id 和 supplier_id 的规则
				this.rules.pay_target = [{ required: true, message: '请输入收款单位' }];
				this.rules.customer_id = [];  // 清除客户的必填规则
				this.rules.supplier_id = [];  // 清除供应商的必填规则
			} else {
				// 如果没有符合的条件，清除所有的验证规则
				this.rules.customer_id = [];
				this.rules.supplier_id = [];
				this.rules.pay_target = [];
			}
		}
	},
	created() {
		// 初始化时根据unit的值设置验证规则
		this.rules.supplier_id = [];  // 清除供应商的必填规则
		this.rules.customer_id = [];  // 清除客户的必填规则
		this.rules.pay_target = [];  // 清除收款单位的必填规则
		this.rules.customer_id = [{required: true, message: '请选择客户'}]
	},
	mounted() {
		this.getAccount()
		this.getType()
		this.getClient()
		this.getApply()
	},
	methods: {
		/**供应商选择事件*/
		supplierChange() {
			this.applyList.map(item => {
				if (this.form.supplier_id == item.apply_id) {
					this.form.pay_target = item.apply_name
				}
			})
		},
		/**客户选择事件*/
		customerChange() {
			this.clientList.map(item => {
				if (this.form.customer_id == item.id) {
					this.form.pay_target = item.customer_name
				}
			})
		},
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit(e) {
			this.form.is_check = e
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = [];
					if (this.form.id) {
						res = await this.$API.finance.sz.edit.post(this.form);
					} else {
						res = await this.$API.finance.sz.add.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 1) {
						this.$emit('success', this.form, this.mode)
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				} else {
					return false;
				}
			})
		},
		//表单注入数据
		async setData(data) {
			this.form.id = data.id
			this.form.other_code = data.other_code
			this.form.other_date = data.other_date
			this.form.pay_type = data.pay_type
			this.form.customer_id = data.customer_id
			this.form.supplier_id = data.supplier_id
			this.form.pay_target = data.pay_target
			this.form.type = data.type && data.type.id
			this.form.settlement_id = data.settlement_id
			this.form.money = data.money
			this.form.enclosure = data.enclosure
			this.form.remark = data.remark
		},
		// 账号
		async getAccount() {
			var res = await this.$API.finance.account.all.get({not_name: '预付款,预收款'});
			this.accountList = res.data;
		},
		// 收入
		async getType() {
			var res = await this.$API.finance.szcate.all.get({type: 2});
			this.typeList = res.data;
		},
		// 客户
		async getClient() {
			var res = await this.$API.client.client.all.get();
			this.clientList = res.data;
		},
		// 供应商
		async getApply() {
			var res = await this.$API.apply.apply.all.get();
			this.applyList = res.data
		},
		radioChange() {
			this.form.customer_id = ""
			this.form.supplier_id = ""
			this.form.pay_target = ""
		},
	}
}
</script>

<style></style>
